package com.example.qinghe.frontController.sale;

import com.example.qinghe.entity.Page;
import com.example.qinghe.entity.communication.Staff;
import com.example.qinghe.entity.sale.Customerqh;
import com.example.qinghe.entity.sale.Opportunityqh;
import com.example.qinghe.service.communication.UserService;
import com.example.qinghe.service.sale.OpportunityqhService;
import com.example.qinghe.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/front/opportunity")
@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials="true")
@Api(tags = "销售管理->商机管理", description = "FrontOpportunityqhController")
public class FrontOpportunityqhController {


    @Autowired
    private OpportunityqhService opportunityqhService;

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("所有商机")
    @GetMapping(value = "/selectOpportunityqhList/{page}/{limit}")
    public Result selectOpportunityqhList(@ApiParam(name = "page", value = "页数") @PathVariable(value = "page")  int page,
                                     @ApiParam(name = "limit", value = "页条数") @PathVariable(value = "limit")  int limit,
                                     @ApiParam(name = "opportunityname", value = "商机名称")  @RequestParam(required = false) String opportunityname,
                                     @ApiParam(name = "companyname", value = "公司名称")  @RequestParam(required = false) String companyname
    ) {

        Page page1 = new Page(page, limit);
        int count = opportunityqhService.countTotall(opportunityname,companyname );
        List<Opportunityqh> opportunityqhs = opportunityqhService.selectOpportunityqhList(page1, page, limit, opportunityname, companyname);
        if (count == 0 || opportunityqhs == null)
            return Result.success(page1, "没有数据");
        System.out.println("fdshjkalfsda"+opportunityqhs);
        page1.setCount(count);
        page1.setData(opportunityqhs);
        return Result.success(page1);
    }

    @ApiOperation("根据商机id查找具体的商机")
    @GetMapping(value = "/selectOpportunityqhById/{id}")
    public Result selectOpportunityqhById(@PathVariable("id") Long id){
        Opportunityqh opportunityqh = opportunityqhService.selectOpportunityqhById(id);
        return opportunityqh==null?Result.error("出现错误"):Result.success(opportunityqh);
    }


    @ApiOperation("查询当前员工的所有商机")
    @GetMapping(value = "/selectOpportunityqhByStaffId")
    public Result selectOpportunityqhByStaffId(HttpServletRequest request){
        Staff staff = userService.getStaff(request);
        if (staff == null)
            return Result.error("请先登录！");
        List<Opportunityqh> opportunityqhs = opportunityqhService.selectOpportunityqhByStaffId(staff.getId());
        return opportunityqhs==null?Result.success("没有数据"):Result.success(opportunityqhs);
    }

}
